﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Linq;
using PostSharp.CodeModel;
using PostSharp.CodeWeaver;
using PostSharp.Collections;
using MicroContainer;

namespace MicroContainer.CompileTime.Generators
{
   internal class DelegatesCacheInitializerFinalizer : ICodeGenerator
   {      
      public void Generate(CodeGenerationContext ctx)
      {
         MethodDefDeclaration staticCnstructor = ctx.ContainerType.Methods.GetOneByName(".cctor");

         InstructionBlock block = staticCnstructor.MethodBody.RootInstructionBlock;
         InstructionWriter writer = new InstructionWriter();
         InstructionSequence seq = staticCnstructor.MethodBody.CreateInstructionSequence();
         block.AddInstructionSequence(seq, NodePosition.After, null);
         writer.AttachInstructionSequence(seq);         
         writer.EmitInstruction(OpCodeNumber.Ret);
         writer.DetachInstructionSequence();

         staticCnstructor.MethodBody.MaxStack = MethodBodyDeclaration.RecomputeMaxStack;
      }
   }
}
